<?php
declare(strict_types=1);

namespace App\Common\Command;

use Core\Basics\BasicsCommand;
use Zms\Unit\Directory;
use Hyperf\Command\Annotation\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

#[Command]
class AppCommand extends BasicsCommand
{

    /**
     * 执行的命令行
     *
     * @var null|string
     */
    protected ?string $name = 'app';


    public function __construct(string $name = null)
    {
        parent::__construct($name);
        $this->setDescription('项目助手');
    }


    public function execute(InputInterface $input, OutputInterface $output): int
    {
        $choice = ['取消', '清理缓存', '清空编译缓存'];
        $handle = $this->choice('请选择操作', $choice, 0);
        switch ($handle) {
            case '清理缓存';
                cache()->clear();
                $this->output->success('缓存已清理');
                break;
            case '取消';
                $this->output->warning('操作已取消');
                break;
            case '清空编译缓存';
                $this->clearContainer();
                break;
        }
        return parent::execute($input, $output);
    }


    public function clearContainer(): void
    {
        if (Directory::remove(BASE_PATH . '/runtime/container')) {
            $this->output->success('编译缓存已删除');
        } else {
            $this->output->warning('编译缓存删除失败');
        }
    }
}